package com.apache.common.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.UtilPlugin;
import com.apache.tools.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 校验机构是否唯一
 * 更新用户信息，添加机构，部门信息
 */
public class UniqueUctOrgOnlyPluginImpl implements CustomMethodPlugin {
    protected Logger log = LoggerFactory.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        String orgEname = String.valueOf(param.get("uctOrg.orgEname"));
        log.info("校验机构唯一性开始："+orgEname);
        String orgId = Validator.generate();


        if (Validator.isEmpty(orgEname)) {
            log.error("缺少uctOrg.orgEname参数");
            throw new BusinessException("缺少参数");
        } else {
            //校验是否重复
            ResultEntity re = UctCoreClient.getOrgByProperty("orgEname", orgEname);
            if (!Validator.isEmpty(re.getEntity()) || re.getResult().equals("false")) {
                throw new BusinessException("机构标识["+orgEname+"]已存在！");
            }else {
                //补充机构和用户信息
                param.put("uctOrg.createTime",Long.toString(System.currentTimeMillis())) ;
                param.put("uctOrg.orgId", orgId);
                param.put("uctUser.orgId",orgId);
                param.put("uctUser.orgEname",orgEname);
                param.put("uctUser.deptId",orgId);
                //补充部门信息
                param.put("uctDept.deptId",orgId);
                param.put("uctDept.deptEname",orgEname+"_manager");
                param.put("uctDept.deptCname","信息管理部");
                param.put("uctDept.deptStatus",1);
                param.put("uctDept.orgId",orgId);
                param.put("uctDept.fatherId","0");
                param.put("uctDept.orgEname",orgEname);
                param.put("uctDept.createTime",Long.toString(System.currentTimeMillis())) ;
            }
        }
        return param;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        String orgId = String.valueOf(param.get("ids"));
        if ( Validator.isNotNull(orgId)) {
            Map<String,Object> deptMap = new HashMap<String,Object>();
            deptMap.put("uctDept.deptId",orgId);
            deptMap.put("uctDept.deptEname",orgId);
            deptMap.put("uctDept.deptCname","会员部");
            deptMap.put("uctDept.deptStatus",1);
            deptMap.put("uctDept.orgId",orgId);
            deptMap.put("uctDept.fatherId","0");
            deptMap.put("uctDept.orgEname",orgId);
            ResultEntity resultEntity = UtilPlugin.getInstance().saveByParams("platform","uctDept",deptMap);
        }
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

    public static void main(String[] args) {
        Date date = new Date();
        String createTime = DateUtils.getTime(date.toString());
        System.out.println(createTime);
    }
}
